package com.ymatou.iapi.sellertrading.testcase;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.DelayAutoReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.YmtOrderFrozenWapper;
/**
*延迟收货
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DelayAutoReceive{
	private static DelayAutoReceiveCall delayautoreceiveCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("延迟收货");
	}

	@Before
	public void caseUp() {
		delayautoreceiveCall=new DelayAutoReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(delayautoreceiveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_DelayAutoReceive_001() {
		Logger.start(true,"延迟收货成功");
		try {
			//TODO 补全case代码
			YmtOrderFrozenWapper ymtOrderFrozenWapper=new YmtOrderFrozenWapper();
			int orderId=ymtOrderFrozenWapper.selectByTradingStatus(3).get(0).getiOrderId();
			int delaydays=2;
			
			//根据orderId获取订单的forzenTime
			Date frozenTime=ymtOrderFrozenWapper.selectOrderFrozenByOrderId(orderId).get(0).getdFrozenTime();
			Logger.debug(frozenTime);
			
			DelayAutoReceiveCall delayAutoReceiveCall=new DelayAutoReceiveCall(3383,3383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("DelayDays", delaydays);
		    updateMap.put("OrderId", orderId);	
		    		  	    
		    //填充参数，并获取JSONObject类型的返回数据
		    JSONObject delayautoreceive=delayAutoReceiveCall.callAndGetReturnData(updateMap);
	    
		    
		    //验证报文
		    int code=delayautoreceive.getInt("Code");
		    Logger.verifyEquals(200, code, "报文返回Code正确");
		    String msg=delayautoreceive.getString("Msg");
		    Logger.verifyIsContains("操作成功", msg, "操作成功");	
		    
		    
		    Thread.sleep( 1000 * 3 );
		    //验证Ymt_Order_Frozen表里dFrozenTime字段
		    
		    //延期自动确认收货时间过后，再根据orderId获取订单的ForzenTime
		    Date updatedFozenTime=ymtOrderFrozenWapper.selectOrderFrozenByOrderId(orderId).get(0).getdFrozenTime();
		    Logger.debug(updatedFozenTime);
		    //计算相差的时间，转换成秒
		    long calFozenTime=(updatedFozenTime.getTime()-frozenTime.getTime())/1000;
		    Logger.debug(calFozenTime);
		    long calday=calFozenTime/(24*3600);
		    long dday= new Long((long)delaydays);

		    Logger.debug(calday);
		    
		    //验证相差的天数=delaydays
		    Logger.verifyEquals(dday,calday , "Delay天数正确");
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_DelayAutoReceive_002() {
		Logger.start(false,"订单号不存在");
		try {
			//TODO 补全case代码

			int orderId=123;
			
			DelayAutoReceiveCall delayAutoReceiveCall=new DelayAutoReceiveCall(3383,3383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("OrderId", orderId);	
		    
		    //填充参数，并获取JSONObject类型的返回数据
  	        //delayautoreceiveCall=new DelayAutoReceiveCall(3383,3383);
		    JSONObject delayautoreceive=delayAutoReceiveCall.callAndGetReturnData(updateMap);
		    
		    //验证报文

		    int code=delayautoreceive.getInt("Code");
		    Logger.verifyEquals(201, code, "报文返回Code正确");
		    String msg=delayautoreceive.getString("Msg");
		    Logger.verifyEquals("订单"+orderId+"不存在", msg, "验证msg");
		    
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_DelayAutoReceive_003() {
		Logger.start(false,"订单未发货，不能做延期处理");
		try {
			//TODO 补全case代码
			YmtOrderFrozenWapper ymtOrderFrozenWapper=new YmtOrderFrozenWapper();
			int orderId=ymtOrderFrozenWapper.selectByTradingStatus(2).get(0).getiOrderId();
			int delaydays=2;
			
			DelayAutoReceiveCall delayAutoReceiveCall=new DelayAutoReceiveCall(3383,3383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("DelayDays", delaydays);
		    updateMap.put("OrderId", orderId);	
		    
		    //填充参数，并获取JSONObject类型的返回数据
		    JSONObject delayautoreceive=delayAutoReceiveCall.callAndGetReturnData(updateMap);
		    
		    //验证报文

		    int code=delayautoreceive.getInt("Code");
		    Logger.verifyEquals(201, code, "报文返回Code正确");
		    String msg=delayautoreceive.getString("Msg");
		    Logger.verifyEquals("订单"+orderId+"未发货,不能做延长收货处理", msg, "验证msg");
		    		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_DelayAutoReceive_004() {
		Logger.start(false,"用户3383不是订单***的所有者");
		try {
			//TODO 补全case代码
			YmtOrderFrozenWapper ymtOrderFrozenWapper=new YmtOrderFrozenWapper();
			int orderId=ymtOrderFrozenWapper.selectByBuyerId(288484).get(0).getiOrderId();
			int delaydays=2;
			
			DelayAutoReceiveCall delayAutoReceiveCall=new DelayAutoReceiveCall(3383,3383);			
		    Map<String, Object> updateMap = new HashMap<String,Object>();
		    updateMap.put("DelayDays", delaydays);
		    updateMap.put("OrderId", orderId);	
		    
		    //填充参数，并获取JSONObject类型的返回数据
		    JSONObject delayautoreceive=delayAutoReceiveCall.callAndGetReturnData(updateMap);
		    
		    //验证报文
		    int code=delayautoreceive.getInt("Code");
		    Logger.verifyEquals(201, code, "报文返回Code正确");
		    String msg=delayautoreceive.getString("Msg");
		    Logger.verifyEquals("用户3383不是订单"+orderId+"的所有者", msg, "验证msg");
		    		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}